import { defineStore } from 'pinia'
import useStorage from '@/hooks/useStorage'

const useWeatherStore = defineStore({
  id: 'weather',
  state: () => {
    return {
      city: '广州',
      data: {}
    }
  },
  actions: {
    changeCity(city) {
      if (!city) return
      this.$patch({
        city
      })
    },
    load(val) {
      console.log(val)
      if (!val) return
      this.$patch({
        data: {
          ...this.$state.data,
          ...val,
        }
      })
    }
  }
})


const instance = useWeatherStore()
const { getItem, setItem } = useStorage()
// set
instance.$subscribe((_, state) => {
  setItem(instance.$id, state)
})
// get
const oVal = getItem(instance.$id)
if (oVal) {
  instance.$patch({ ...oVal })
}

export default useWeatherStore
